﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.WVB.Framework;
using Microsoft.WVB.Silverlight.Infrastructure;

namespace Microsoft.WVB.UI
{
    [TemplateVisualState(GroupName = VisualStates.GroupFade, Name = VisualStates.StateFadesIn),
    TemplateVisualState(GroupName = VisualStates.GroupFade, Name = VisualStates.StateFadesOut)]
    [TemplatePart(Name = LargeSpinner.ScaleTransformElementName, Type = typeof(ScaleTransform)),
    TemplatePart(Name = LargeSpinner.TranslateTransformElementName, Type = typeof(TranslateTransform))]
    public class LargeSpinner : Control
    {
        private const string ScaleTransformElementName = "ScaleTransformElement";
        private const string TranslateTransformElementName = "TranslateTransformElement";

        private TranslateTransform TranslateTransformObject = null;
        private ScaleTransform ScaleTransformObject = null;

        public Boolean IsActive
        {
            get { return (Boolean)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }

        public static readonly DependencyProperty IsActiveProperty = null;

        public Double Diameter
        {
            get { return (Double)GetValue(DiameterProperty); }
            set { SetValue(DiameterProperty, value); }
        }

        public static readonly DependencyProperty DiameterProperty = null;

        static LargeSpinner()
        {
            DiameterProperty = DependencyProperty.Register("Diameter", typeof(Double), typeof(LargeSpinner), new PropertyMetadata(new PropertyChangedCallback(OnDiameterChanged)));
            IsActiveProperty = DependencyProperty.Register("IsActive", typeof(Boolean), typeof(LargeSpinner), new PropertyMetadata(new PropertyChangedCallback(OnIsActiveChanged)));
        }

        static void OnDiameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((LargeSpinner)d).OnDiameterChanged((Double)e.OldValue, (Double)e.NewValue);
        }

        protected virtual void OnDiameterChanged(Double oldValue, Double newValue)
        {
            if (oldValue == newValue)
                return;

            if ((this.ApplyTemplate()) && 
                (TranslateTransformObject != null) &&
                (ScaleTransformObject != null))
            {
                Double areaSize = 100.0;
                Double scale = this.Diameter / 100.0;
                Double translate = (areaSize - this.Diameter) / (2.0 * scale + 0.001);

                TranslateTransformObject.X = translate;
                TranslateTransformObject.Y = translate;

                ScaleTransformObject.ScaleX = scale;
                ScaleTransformObject.ScaleY = scale;
            }
        }

        static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((LargeSpinner)d).OnIsActiveChanged((Boolean)e.OldValue, (Boolean)e.NewValue);
        }

        protected virtual void OnIsActiveChanged(Boolean oldValue, Boolean newValue)
        {
            if (oldValue == newValue)
                return;

            UpdateVisualState();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            TranslateTransformObject = base.GetTemplateChild(LargeSpinner.TranslateTransformElementName) as TranslateTransform;
            ScaleTransformObject = base.GetTemplateChild(LargeSpinner.ScaleTransformElementName) as ScaleTransform;
        }

        public LargeSpinner()
        {
            this.IsHitTestVisible = false;
            this.DefaultStyleKey = typeof(LargeSpinner);          
        }

        internal void UpdateVisualState()
        {
            this.ChangeVisualState(true);
        }

        private void ChangeVisualState(bool useTransitions)
        {
            if (this.IsActive)
            {
                VisualStates.GoToState(this, useTransitions, new String[] { VisualStates.StateFadesIn });
            }
            else
            {
                VisualStates.GoToState(this, useTransitions, new String[] { VisualStates.StateFadesOut });
            }
        }
    }
}
